package com.lagou.edu.message.mq.listener;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.common.constant.MQConstant;
import com.lagou.edu.common.mq.dto.BaseMqDTO;
import com.lagou.edu.common.mq.listener.AbstractMqListener;
import com.lagou.edu.common.util.ValidateUtils;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.message.api.dto.CourseStatusReleaseDTO;
import com.lagou.edu.message.api.dto.LessonStatusReleaseDTO;
import com.lagou.edu.message.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lagou.edu.message.api.dto.Message;
/**
 * @Author: Hugh
 * @Date: 2021/1/2
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = MQConstant.Topic.COURSE_STATUS_RELEASE, consumerGroup = "${rocketmq.producer.group}" + "_" + MQConstant.Topic.COURSE_STATUS_RELEASE)
public class CourseStatusReleaseListener extends AbstractMqListener<BaseMqDTO<CourseStatusReleaseDTO>> implements RocketMQListener<BaseMqDTO<CourseStatusReleaseDTO>> {

    @Autowired
    protected CourseRemoteService courseRemoteService;
    @Autowired
    private IMessageService messageService;

    @Override
    public void onMessage(BaseMqDTO<CourseStatusReleaseDTO> data) {
        log.info("onMessage {}- data:{}",MQConstant.Topic.COURSE_STATUS_RELEASE, JSON.toJSONString(data));
        ValidateUtils.notNullParam(data);
        ValidateUtils.notNullParam(data.getMessageId());

        if(this.checkMessageId(data.getMessageId())){
            return;
        }

        Integer courseId = data.getData().getLessonId();
        CourseDTO courseById = courseRemoteService.getCourseById(courseId, 0);
        Message message = new Message();
        message.setContent(courseById.getCourseName()+ "课程新上线，欢迎试学。");
        messageService.sendMessageToEveryOne(message);
    }
}
